﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLPropertyGrid
{
    public static class Extensions
    {


        public static object FindResource(string name)
        {
            if (Application.Current.Resources.Contains(name))
            {
                return Application.Current.Resources[name];
            }
            else
            {
                FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
                return root.FindResource(name);
            }
        }
        internal static object FindResource(this FrameworkElement root, string name)
        {
            if (root == null) return null;
            if (root != null && root.Resources.Contains(name))
            {
                return root.Resources[name];
            }
            else
            {
                try
                {
                    object ret = root.FindName(name);
                    if (ret != null)
                        return ret;
                    FrameworkElement rec = VisualTreeHelper.GetParent(root) as FrameworkElement;
                    return rec.FindResource(name);
                }
                catch
                {
                }
            }

            return null;
        }
    }
}
